/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.codeview.CodeDemo;
import com.cv4j.codeview.CodeView;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.filters.NatureFilter;

/**
 * 使用NatureFilter滤镜
 *
 * @since 2021-05-18
 */
public class DslAbilitySlice extends BaseAbilitySlice {
    private Image image;
    private CodeView codeview2;
    private CodeView codeview4;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_dsl;
    }

    @Override
    protected void initComponents(Intent intent) {
        codeview2 = (CodeView) findComponentById(ResourceTable.Id_codeview2);
        codeview4 = (CodeView) findComponentById(ResourceTable.Id_codeview4);
        if ("< Rxjava来操作滤镜".equals(getTitle().getText())) {
            initViewNature();
        } else {
            initViewDsl();
        }
    }

    /**
     * 布局初始化
     */
    private void initViewNature() {
        codeview2.setVisibility(Component.VISIBLE);
        codeview4.setVisibility(Component.HIDE);
        image = (Image) findComponentById(ResourceTable.Id_image);
        CV4JImage cv4Jimage =
            new CV4JImage(PixelMapUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_test_io).get());
        image.setPixelMap(
            new NatureFilter().filter(cv4Jimage.getProcessor()).get().getImage().toBitmap());
        codeview2.showCode(CodeDemo.getDemo5());
    }

    private void initViewDsl() {
        codeview2.setVisibility(Component.HIDE);
        codeview4.setVisibility(Component.VISIBLE);
        image = (Image) findComponentById(ResourceTable.Id_image);
        CV4JImage cv4Jimage =
            new CV4JImage(PixelMapUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_test_io).get());
        image.setPixelMap(new NatureFilter().filter(cv4Jimage.getProcessor()).get().getImage().toBitmap());
        codeview4.showCode(CodeDemo.getDemo4());
    }
}

